package com.example.team6.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.team6.entity.*;
import com.example.team6.entity.resultt;
//import com.example.team6.service.AdminService;
import com.example.team6.mapper.TeacherMapper;
import com.example.team6.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wwj
 * @since 2020-06-28
 */
@RestController
@CrossOrigin
//@RequestMapping("/team6/teacher")
public class TeacherController {
    @Autowired
TeacherService teacherService;
  @Autowired
    TeacherMapper teacherMapper;

    @PostMapping(value = "api/teaclogin")
    @ResponseBody
    public resultt teaclogin(@RequestBody Teacher requestUser) {
        resultt resultt = new resultt();
        QueryWrapper<Teacher> teacherWrapper = new QueryWrapper<>();
        teacherWrapper.eq("teac_account",requestUser.getTeacAccount());
        //System.out.println(requestUser.getAdmAccount());
        teacherWrapper.eq("teac_pass", requestUser.getTeacPass());

        Teacher teacher = teacherService.getOne(teacherWrapper);
        //System.out.println(teacher);
        if (null == teacher) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("teacherid", teacher.getTeacherid());
            resultt.setData(map);
        }
        return resultt;
    }
    @PostMapping(value = "api/teacregister")
    @ResponseBody
    public resultt sturegister (@RequestBody Teacher requestUser){

        resultt resultt = new resultt();

        Teacher teacher = new Teacher();
        teacher.setTeacAccount(requestUser.getTeacAccount());
        teacher.setTeacPass(requestUser.getTeacPass());
        teacher.setTeachername(requestUser.getTeachername());
        Integer result = teacherMapper.insert(teacher);

        if (null == result) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("adminid", teacher.getTeacherid());
            resultt.setData(map);
        }
        return resultt;
    }

    @PostMapping(value = "api/teacpersion")
    @ResponseBody
    public List<Teacher> teacpersion (@RequestBody Teacher requestUser) {

        System.out.println(requestUser.getTeacherid());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("teacherid", requestUser.getTeacherid());

        List<Teacher> teacherList = teacherMapper.selectByMap(map);

        return teacherList;
    }

    @PostMapping(value = "api/teacherupdata")
    @ResponseBody
    public resultt teacherupdata (@RequestBody Teacher requestUser){

        resultt resultt = new resultt();
        Teacher teacher = new Teacher();
        teacher.setTeacAccount(requestUser.getTeacAccount());
        teacher.setTeacPass(requestUser.getTeacPass());
        teacher.setExperience(requestUser.getExperience());
        teacher.setField(requestUser.getField());
        teacher.setTeachername(requestUser.getTeachername());
        teacher.setTeacherid(requestUser.getTeacherid());

        Integer result = teacherMapper.updateById(teacher);

        if (null == result) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("teacherid", teacher.getTeacherid());
            resultt.setData(map);
        }
        return resultt;
    }

}




